package com.yonyou.pmclouds.businessobject.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst4NCC;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.businessobject.rmiitf.BusinessObjectMaintain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping({"/outter/bo"})
public class BusinessObjectController4NCC {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private BusinessObjectMaintain businessObjectMaintain;

    @GetMapping("/ncc/getBillUrl")
    public String getBillUrl(@RequestParam("billType") String billType,
                                     @RequestParam("pkBill") String pkBill ) throws BusinessException {

        String pmcType = BillTypeConst4NCC.getNccToPMCMap().get(billType);

        String tenantId = RuntimeEnvironment.getTenantId();

        String printCode = businessObjectMaintain.getPrintCodeByType(tenantId, pmcType, null);

        if(StringUtils.isEmpty(printCode))
            return "";
        String linkQuery = "/outter/print/preview?serverUrl=/outter/billView/queryBillDetail"
                + "&printcode={printcode}"
                + "&tenantId={tenantId}"
                + "&sendType=2"
                + "&params=%7B%22pkBill%22:%22{pkBill}%22,"
                + "%20%22billType%22:%22{billtype}%22%7D";;
        linkQuery = linkQuery.replace("{printcode}", printCode);
        linkQuery = linkQuery.replace("{tenantId}",tenantId);
        linkQuery = linkQuery.replace("{pkBill}",pkBill);
        linkQuery = linkQuery.replace("{billtype}", pmcType);

        return linkQuery;
    }
}
